﻿using ConfExpert.Utility;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class GirisPartisi : XPopupForm
    {
        public GirisPartisi(string GirenAksesuarlar)
        {
            InitializeComponent();
            this.GirenAksesuarlar = GirenAksesuarlar;
        }

        string GirenAksesuarlar;
        private void GirisPartisi_Load(object sender, EventArgs e)
        {
            chkIdle.CheckedChanged += chkIdle_CheckedChanged;

            GetDetails("SODetay", "GetOrderRefforAksesuar");

            gleOrderRef.Properties.Assign(GetIntellisensedRI(ds.SODetay));
            gleOrderRef.Properties.ProcessNewValue += new ProcessNewValueEventHandler(ri_ProcessNewValue2);
            gleOrderRef.Properties.View.Columns["BrandId"].ColumnEdit = RI(Rp.Brand);
            gleOrderRef.Properties.View.Columns["SezonId"].ColumnEdit = RI(Rp.Sezon);
            gleOrderRef.EditValueChanged += gleOrderRef_EditValueChanged;

            string harekettipleri = "4,5,7,8,18,19,34";

            DataTable dt = AppSession.Lookups.HareketTipi.Select("HareketTipiId in (" + harekettipleri + ")").CopyToDataTable();
            RepositoryItemGridLookUpEdit riCikisTipi = GetCustomGLE(dt, "Name", "HareketTipiId");
            gleGirisTipi.Properties.Assign(riCikisTipi);
            gleGirisTipi.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;

            gleBrand.Properties.Assign(RI(Rp.Brand)); 
            gleSezon.Properties.Assign(RI(Rp.Sezon));

            gle.Properties.Assign(RI(Rp.Cari));
            gleKaynak.EditValue = 1;

            gleKaynak.Properties.Assign(GetGlKaynak());
            gleKaynak.EditValueChanged += new EventHandler(gleKaynak_EditValueChanged);
            gleKaynak.Properties.TextEditStyle = TextEditStyles.DisableTextEditor;
            this.Shown += new EventHandler(CikisEmri_Shown);

            gleGirisTipi.EditValueChanged += gleGirisTipi_EditValueChanged;
            btnOk.Click += new EventHandler(btnOk_Click);
            btnCancel.Click += new EventHandler(btnCancel_Click);
        }

        void gleGirisTipi_EditValueChanged(object sender, EventArgs e)
        {
            gle.Enabled =
            gleKaynak.Enabled =
            txtIrsaliyeNo.Enabled = gleGirisTipi.EditValue.ToInt() != 34;
        }

        void gleOrderRef_EditValueChanged(object sender, EventArgs e)
        {
            if (gleOrderRef.Properties.View.GetFocusedRow() != null)
            {
                DataRow dr = ((DataRowView)gleOrderRef.Properties.View.GetFocusedRow()).Row;
                gleBrand.EditValue = intParse(dr["BrandId"]);
                gleSezon.EditValue = intParse(dr["SezonId"]);
            };
        }

        void chkIdle_CheckedChanged(object sender, EventArgs e)
        {
            gleOrderRef.Enabled = !chkIdle.Checked;
            if (chkIdle.Checked)
                gleOrderRef.EditValue = null;
        }

        void gleKaynak_EditValueChanged(object sender, EventArgs e)
        {
            gle.Enabled = true;
            gle.EditValue = null;

            switch (intParse(gleKaynak.EditValue))
            {
                case 1: //Tedarikçi 
                    gle.Properties.Assign(RI(Rp.V_CariV));
                    break;
                case 2: //Müşteri
                    gle.Properties.Assign(RI(Rp.V_CariC));
                    break;
                case 4: //Atölye
                    gle.Properties.Assign(RI(Rp.V_CariA));
                    break;
            }
        }

        void CikisEmri_Shown(object sender, EventArgs e)
        {
            gle.Focus();
        }       


        void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        void btnOk_Click(object sender, EventArgs e)
        {
            if (gleGirisTipi.EditValue.ToInt() == 0)
            {
                XtraMessageBox.Show("Giriş Tipi seçiniz!");
                return;
            }
            if (gleOrderRef.Text.IsNullOrWhiteSpace() && !chkIdle.Checked)
            {
                XtraMessageBox.Show("OrderRef bilgisini girin. OrderRef yok ise idle kutucuğunu işaretleyin!");
                return;
            }
            if (intParse(gleBrand.EditValue) == 0)
            {
                XtraMessageBox.Show("Marka boş olamaz!");
                return;
            }
            if (intParse(gleSezon.EditValue) == 0)
            {
                XtraMessageBox.Show("Sezon boş olamaz!");
                return;
            }

            if (gle.EditValue == null && gleGirisTipi.EditValue.ToInt() != 34)
            {
                ShowErrorMessage("Bir geliş yeri seçmelisiniz.");
                gle.Focus();
                return;
            }

            ExecuteNonQuery("InsAksesuarGiris", GirenAksesuarlar, gleOrderRef.Text, gleBrand.EditValue, gleSezon.EditValue, txtIrsaliyeNo.Text, intParse(gleKaynak.EditValue), intParse(gle.EditValue), WhoAmI, memoEdit1.Text,gleGirisTipi.EditValue);
            this.DialogResult = DialogResult.OK;
        }
    }
}